/************************************************************************/
// /*                          通用交互js方法                              */========================================================================================
/************************************************************************/
var thisDom;
var winHeight;
var winScrollTop;
var domain='http://imageqiniu.xxxxxbbs.com/';//图片服务器域名
var vdomain='http://videoqiniu.xxxxxbbs.com/';//视频服务器域名
var imgCommentCount=0;


var t=[];


//字数判定方法
function charCount(len,v){
	thisValue=thisDom.val();
	len = 0;
	v = $.trim(thisValue);
	for(i=0;i<v.length;i++)	{
		if(v.charCodeAt(i)>256)	{
			len += 2;
		} else {
			len++;
		}
	}
	thisLength=len;
}
//计数转换
function countFormat(thisCount){
    thisCount=thisCount.toString();
    if(thisCount.length>4){
        var startCut=0;
        var cutLength=thisCount.length-4;
        var wan;
        var qian;
        wan=thisCount.substr(startCut,cutLength);
        qian=thisCount.substr(thisCount.length-4,1);    
        if(qian>0){
            thisCount=wan+'.'+qian+'万';
        }else{
            thisCount=wan+'万';
        }
    }
    return thisCount;
}
//自定义弹出对话框
function dialog(content,confirmText,confirmBackground,dialogTop,cancelDisplay,ok){
	var html='<div class="dialog-layer">'+
				'<div id="dialog">'+
					'<p></p>'+
					'<div class="button-box"><a href="javascript:;" class="cancel">取消</a> <a href="javascript:;" class="confirm"></a></div>'+
				'</div>'+
			'</div>';
	$('body').prepend(html);
    $('body').addClass('body-overlay');
	$(".dialog-layer").fadeIn(200);
	$("#dialog").find('p').text(content);
	$("#dialog").find('.confirm').text(confirmText);
	$("#dialog").find('.confirm').css({
		'background':confirmBackground,
		'border-color':confirmBackground
	});
	$("#dialog").find('.cancel').css('display',cancelDisplay);
	$("#dialog").css({
		'top': dialogTop+'px'
	});
	$("body").off("click","#dialog .confirm").on("click","#dialog .confirm",function(event) {
		$(".dialog-layer").fadeOut(200,function(){
            if($('#overlay').length<=0){
                if($(".sort-layer").length>0){
                    if($(".sort-layer").is(":hidden")){
                        $('body').removeClass('body-overlay');
                    }
                }else{
                    $('body').removeClass('body-overlay');
                }
            }
			$(".dialog-layer").remove();
			ok();
		});
	});
	$("body").off("click","#dialog .cancel").on("click","#dialog .cancel",function(event) {
		$(".dialog-layer").fadeOut(200,function(){
            if($('#overlay').length<=0){
                if($(".sort-layer").length>0){
                    if($(".sort-layer").is(":hidden")){
                        $('body').removeClass('body-overlay');
                    }
                }else{
                    $('body').removeClass('body-overlay');
                }
            }
            $(".dialog-layer").remove();
            //激活发帖自动保存
            if($("#page").val()=="thread-input" || $("#page").val()=="thread-update"){
                autoSave=setInterval(function(){
                        autoSaveFun();
                },3000);
            }
		});
	});
}
//提交内容过滤    
var htmlRegLeft = new RegExp("<", "gi");
var htmlRegRight = new RegExp(">", "gi");
function submitFilter(){
    $("textarea").each(function(index, el) {
        var inputValue=$(this).val();
        // inputValue=inputValue.replace(htmlRegLeft,"&lt;");
        // inputValue=inputValue.replace(htmlRegRight,"&gt;");
        //var spaceReg= /^\s*$/;
        //if(spaceReg.test(inputValue)){
        // inputValue=inputValue.replace(/&nbsp;/ig, ""); 
        if($("#page").val()==""){
            inputValue=inputValue.replace(/\s*$/g, "");  
        }else{
            inputValue=inputValue.replace(/(^\s*)|(\s*$)/g, "");  
        }
        //}
        $(this).val(inputValue);
    }); 
    $("input[type=text]").each(function(index, el) {
        var inputValue=$(this).val();
        // inputValue=inputValue.replace(htmlRegLeft,"&lt;");
        // inputValue=inputValue.replace(htmlRegRight,"&gt;");
        // inputValue=inputValue.replace(/&nbsp;/ig, ""); 
        // inputValue=inputValue.replace(/\s/ig, "");  
        $(this).val(inputValue);
    }); 
}
//输入字数限制
function commentCount(charLen){
    charCount();
    var charLength=Math.round(thisLength/2);
    if(thisLength>charLen  && thisLength!=0){
        thisDom.next(".button-box").find('.char-limit em').text('已超出');
        thisDom.next(".button-box").find('.char-limit i').text(Math.abs(charLen/2-charLength));
        thisDom.next(".button-box").find('.char-limit i').css('color','#e24343');  
        thisDom.next(".button-box").find('.char-limit b').text('字');
        thisDom.addClass('textarea-error');   
    }
    else{
        thisDom.next(".button-box").find('.char-limit em').text('还可以输入');
        thisDom.next(".button-box").find('.char-limit i').text(charLen/2-charLength);
        thisDom.next(".button-box").find('.char-limit i').css('color','#2175d0');
        thisDom.next(".button-box").find('.char-limit b').text('字');
        thisDom.removeClass('textarea-error');
    }
}
//禁言对话框提示
function forbiddenAlert(){
    content='您好，您尚未登录或账号处于禁言状态，无法进行此操作。';
    dialogTop=200;
    confirmText='确定';
    confirmBackground="#e97171";
    cancelDisplay='none';
    ok=function(){return false;};
    dialog(content,confirmText,confirmBackground,dialogTop,cancelDisplay,ok);
}



//回复等图片加载方法
function uploadReplyImg(pickfiles){
   var replyUploader = Qiniu.uploader({
        runtimes: 'html5,flash,html4',
        browse_button: pickfiles,
        filters: {
          mime_types : [ //只允许上传图片
            { title : "Image files", extensions : "jpg,gif,png,jpeg" }
          ],
        },
        flash_swf_url: '/plugin/Moxie.swf',
        dragdrop: true,
        chunk_size: '4mb',
        uptoken_url: $('#uptoken_url').val(),
        uptoken_url1: $('#uptoken_url1').val(),
        domain: $('#domain').val(),
        save_key: true,
        auto_start: true,
        init: {
           'FilesAdded': function(up, files) {
                thisDom=$('#'+pickfiles);
                commentImgBox();
            },
            'BeforeUpload': function(up, file) {

            },
            'UploadProgress': function(up, file) {
                
            },
            'UploadComplete': function() {
                
            },
            'FileUploaded': function(up, file, info) {
                res=$.parseJSON(info);
                commentImgUploaded(res);
            },
            'Error': function(up, err, errTip) {
            },
            'Key': function(up, file) {
                 var key = "";
                 return key
             }
        }
    });
}
//添加图片后
function commentImgBox(){
    thisDom.parent(".button-box").next('.comment-img-area').remove();
    var left= thisDom.parent(".button-box").find('.upload-img-btn').position().left+7;
    var html='<div class="comment-img-area" style="left:'+left+'px;margin-top:10px;display:none">'+
                '<div class="comment-img-box">'+
                    '<span><em></em><b></b></span>'+
                    '<a href="javascript:;" class="close">×</a>'+
                    '<div class="img-box">'+
                        '<img src="/images/loading.gif" class="upload-img"/>'+
                    '</div>'+
                '</div>'+
             '</div>';
    thisDom.parent(".button-box").after(html);
    $(".comment-img-area").fadeIn(200);
    $("body").on('click', '.comment-img-area .close', function(event) {
        thisDom=$(this);
        $(this).parents(".comment-img-area").fadeOut(200, function() {
             thisDom.parents(".comment-img-area").remove();
        });
    });
}
//图片上传完成后
function commentImgUploaded(res){
    thisDom.parent(".button-box").next(".comment-img-area").attr({
        'imghash':res.hash,
        'imgurl':res.hash,
        'imgmime':res.mimeType,
        'imgsize':res.fsize,
        'imgwidth':res.w,
        'imgheight':res.h
    });
     thisDom.parent(".button-box").next(".comment-img-area").find('.upload-img').attr("src",domain+res.hash+"?imageMogr2/auto-orient/thumbnail/!80x80r/gravity/Center/crop/80x80/quality/100");
}


//表情包
function faceImg(){
  var reg=/(\[5X:)([0-9]*)(\])/g;
  $("p").each(function(index, el) {
    var pText=$(this).html();
    pText=pText.replace(reg,'<img src="/images/5xface/$2.png" class="xxxxx-face"/>');
    $(this).html(pText)
  });
}


/************************************************************************/
/*                          窗口尺寸变化                                */
/************************************************************************/
// 帖子最终页右侧固定方法
function threadRight(){
    var threadRightHeight=$(".thread-right").height();
    var threadRightUserInfoHeight=$(".thread-right").find('.user-info').height();
    var threadRightGoodsHeight=$(".thread-right").find('.goods').height();
    var threadRightTmallHeight=$(".thread-right").find('.tmall').height();
    if(winHeight>threadRightUserInfoHeight+350){
        if(winScrollTop>=threadRightHeight+100){
            $(".thread-right .user-info").addClass('user-info-fixed');
            if(winHeight> threadRightUserInfoHeight+threadRightGoodsHeight+350){
                $(".thread-right").find('.goods').addClass('goods-fixed');
            }else{
                $(".thread-right").find('.goods').removeClass('goods-fixed');
            }
            if(winHeight< threadRightUserInfoHeight+threadRightGoodsHeight+350 && winHeight>threadRightUserInfoHeight+threadRightTmallHeight+350){
                $(".thread-right").find('.tmall').addClass('tmall-fixed');
            }else{
                $(".thread-right").find('.tmall').removeClass('tmall-fixed');
            }
            if($('.goods').length<1 && winHeight>threadRightUserInfoHeight+threadRightTmallHeight+350){
                $(".thread-right").find('.tmall').addClass('tmall-fixed');
            }
        }
        else{
            $(".thread-right .user-info").removeClass('user-info-fixed');
            $(".thread-right").find('.goods').removeClass('goods-fixed');
            $(".thread-right").find('.tmall').removeClass('tmall-fixed');
        }
    }else{
        $(".thread-right .user-info").removeClass('user-info-fixed');
        $(".thread-right").find('.goods').removeClass('goods-fixed');
        $(".thread-right").find('.tmall').removeClass('tmall-fixed');
    }
}
window.onresize=function(){
    winHeight=$(window).height();
    if($('.thread-right').length>0){
        threadRight();
    }
}


